/**
 * Created on 25 dec. 2011 at 12:17:36 by HozakaN
 */
package fr.castorhoz.isi_connectivity_widget.mobiledata_widget;

import java.lang.reflect.Method;

import android.content.Context;
import android.telephony.TelephonyManager;

/**
 * @author HozakaN
 * 
 */
public class MobileDataSwitcherFroyoAndPrevious extends MobileDataSwitcher {

	/* (non-Javadoc)
	 * @see fr.castorhoz.isi_connectivity_widget.mobiledata_widget.MobileDataSwitcher#enableNetwork()
	 */
	@Override
	public boolean enableNetwork(Context context) {
		
		TelephonyManager m_telManager = (TelephonyManager) context.getSystemService("phone");
		boolean bRes = false;
		try
        {
          // Will be used to invoke hidden methods with reflection
	        Class<? extends TelephonyManager> cTelMan = null;
	        Method getITelephony = null;
	        Object oTelephony = null;
	        Class<? extends Object> cTelephony = null;
	        Method action = null;
    		
	        // Get the current object implementing ITelephony interface
	        cTelMan = m_telManager.getClass();
	        getITelephony = cTelMan.getDeclaredMethod("getITelephony");
	        getITelephony.setAccessible(true);
	        oTelephony = getITelephony.invoke(m_telManager);
	        
	        // Call the enableDataConnectivity/disableDataConnectivity method
	        // of Telephony object
	        cTelephony = oTelephony.getClass();
            action = cTelephony.getMethod("enableDataConnectivity");
	        action.setAccessible(true);
	        bRes = (Boolean)action.invoke(oTelephony);
        }
        catch (Exception e)
	    {
            bRes = false;
        }
        //FIXME guillaume sending back bRes says the method have been invoked, not the network state has change
        return bRes;
	}

	/* (non-Javadoc)
	 * @see fr.castorhoz.isi_connectivity_widget.mobiledata_widget.MobileDataSwitcher#disableNetwork()
	 */
	@Override
	public boolean disableNetwork(Context context) {
		TelephonyManager m_telManager = (TelephonyManager) context.getSystemService("phone");
		boolean bRes = false;
		try
        {
          // Will be used to invoke hidden methods with reflection
	        Class<? extends TelephonyManager> cTelMan = null;
	        Method getITelephony = null;
	        Object oTelephony = null;
	        Class<? extends Object> cTelephony = null;
	        Method action = null;
    		
	        // Get the current object implementing ITelephony interface
	        cTelMan = m_telManager.getClass();
	        getITelephony = cTelMan.getDeclaredMethod("getITelephony");
	        getITelephony.setAccessible(true);
	        oTelephony = getITelephony.invoke(m_telManager);
	        
	        // Call the enableDataConnectivity/disableDataConnectivity method
	        // of Telephony object
	        cTelephony = oTelephony.getClass();
	        action = cTelephony.getMethod("disableDataConnectivity");
	        action.setAccessible(true);
	        bRes = (Boolean)action.invoke(oTelephony);
        }
        catch (Exception e)
	    {
            bRes = false;
        }
        //FIXME guillaume sending back bRes says the method have been invoked, not the network state has change
        return bRes;
	}

}
